package week12;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;

//
@EnableAspectJAutoProxy
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
    public static void main(String[] args) throws InterruptedException {
        //docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15672:15672 -p 5672:5672 -p 25672:25672 -p 61613:61613 -p 1883:1883 rabbitmq:management
        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(DemoApplication.class)
                .web(WebApplicationType.NONE).run(args);

        KafkaTemplate kafkaTemplate = ctx.getBean(KafkaTemplate.class);
        System.out.println(kafkaTemplate);
        kafkaTemplate.send(new ProducerRecord<String, String>("TestTopic1", 2, "", "vvvvvv"));
    }

    @KafkaListener(topics = "TestTopic1")
    public void receive(String message) {
        System.out.println("接收到kafka消息系统的message:" + message);
    }
}
